//
//  ContainerViewController.swift
//  Yidingzhi
//
//  Created by flybywind on 15/12/7.
//  Copyright © 2015年 flybywind. All rights reserved.
//

import UIKit


class ContainerViewController: UIViewController {

    let fabricWidth = 0.4
    let animDuration = 1.0
    @IBOutlet weak var sceneView: UIView!
    weak var scnViewController: GameViewController!
    weak var fabricViewController: FabricTableViewController!
    weak var fabricView : UIView?
    
    var animating = false
    override func viewDidLoad() {
        super.viewDidLoad()
        
        scnViewController = self.childViewControllers.first as! GameViewController
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    func showFabric(sss: SuitStatus) {
        if animating {return}
        animating = true
        
        let scnFrame = sceneView.frame
        log.debug("scene view frame:\(scnFrame.origin) \(scnFrame.size)")

        var fw = fabricWidth
        let orientation = UIApplication.sharedApplication().statusBarOrientation
        if fabricViewController == nil {
            let sb = UIStoryboard.init(name: "Main", bundle: nil)
            fabricViewController = sb.instantiateViewControllerWithIdentifier("fabricView") as? FabricTableViewController
            
            fabricView = fabricViewController.view
            self.addChildViewController(fabricViewController!)
            fabricViewController.didMoveToParentViewController(self)
            
            self.view!.addSubview(fabricView!)
        }
        
        if orientation == .Portrait || orientation == .PortraitUpsideDown {
            fw = 1
            fabricView?.frame = CGRect(origin: scnFrame.origin, size: scnFrame.size)
            log.debug("fabric view frame:\(fabricView?.frame.origin) \(fabricView!.frame.size)")
        }else {
            fabricView?.frame = CGRect(origin: scnFrame.origin,
                size: CGSize(width: scnFrame.width * CGFloat(fw), height: scnFrame.height))
            log.debug("fabric view frame:\(fabricView?.frame.origin) \(fabricView!.frame.size)")
        }
        fabricViewController.receiveData = sss
        fabricViewController.reloadTable()
        fabricView!.center = scnFrame.origin
        fabricView!.layer.anchorPoint = CGPoint(x: 0, y: 0)
        fabricView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI/2))
        fabricView!.alpha = 0
        
        self.fabricView!.hidden = false
        
        if orientation != .Portrait && orientation != .PortraitUpsideDown {
            self.scnViewController.moveModelHorizontal(Float(fw/2), animDuration: animDuration)
        }
        
        UIView.animateWithDuration(animDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: [],
            animations: {[unowned self] in
                self.fabricView!.transform = CGAffineTransformIdentity
                self.fabricView!.alpha = 1
            }, completion: {[unowned self] _ in
                self.fabricView!.hidden = false
                self.animating = false
            })
        
    }
    
    func hideFabric() {
        let orientation = UIApplication.sharedApplication().statusBarOrientation

        if fabricView != nil && !fabricView!.hidden && !animating {
            animating = true
            if orientation != .Portrait && orientation != .PortraitUpsideDown {
                scnViewController.resetModelHorizontal(self.animDuration)
            }
            UIView.animateWithDuration(animDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: [],
                animations: {[unowned self] in
                    self.fabricView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI/2))
                    self.fabricView?.alpha = 0
                }, completion: {[unowned self] _ in
                    self.fabricView?.hidden = true
                    // transform一定要清除，否则设置frame的时候会出问题！
                    self.fabricView!.transform = CGAffineTransformIdentity
                    self.animating = false
                })
        }
    }
}
